layui.use(['form', 'layer', 'layedit', 'upload'], function () {
    const form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        upload = layui.upload,
        layedit = layui.layedit,
        $ = layui.jquery;

    //封面图上传
    let coverUrl = null;
    const uploadInst = upload.render({
        elem: '#test1'
        , url: host + '/a03/umsmember/uploadImage'
        , method: 'post'  //可选项。HTTP类型，默认post
        , field: 'multipartFile'
        , before: function (obj) {
            //预读本地文件示例，不支持ie8
            obj.preview(function (index, file, result) {
                $('#demo1').attr('src', result); //图片链接（base64）
            });
        }
        , done: function (res) {
            //如果上传失败
            if (res.code !== 200) {
                return layer.msg('上传失败');
            } else {
                //上传成功
                coverUrl = res.data;
            }
        }
        , error: function () {
            //演示失败状态，并实现重传
            const demoText = $('#demoText');
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
            demoText.find('.demo-reload').on('click', function () {
                uploadInst.upload();
            });
        }
    });

    //封面图上传2
    let coverUrl2 = null;
    const uploadInst2 = upload.render({
        elem: '#test2'
        , url: host + '/a03/umsmember/uploadImage'
        , method: 'post'  //可选项。HTTP类型，默认post
        , field: 'multipartFile'
        , before: function (obj) {
            //预读本地文件示例，不支持ie8
            obj.preview(function (index, file, result) {
                $('#demo2').attr('src', result); //图片链接（base64）
            });
        }
        , done: function (res) {
            //如果上传失败
            if (res.code !== 200) {
                return layer.msg('上传失败');
            } else {
                //上传成功
                coverUrl2 = res.data;
            }
        }
        , error: function () {
            //演示失败状态，并实现重传
            const demoText = $('#demoText');
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
            demoText.find('.demo-reload').on('click', function () {
                uploadInst2.upload();
            });
        }
    });

    //封面图上传3
    let coverUrl3 = null;
    const uploadInst3 = upload.render({
        elem: '#test3'
        , url: host + '/a03/umsmember/uploadImage'
        , method: 'post'  //可选项。HTTP类型，默认post
        , field: 'multipartFile'
        , before: function (obj) {
            //预读本地文件示例，不支持ie8
            obj.preview(function (index, file, result) {
                $('#demo3').attr('src', result); //图片链接（base64）
            });
        }
        , done: function (res) {
            //如果上传失败
            if (res.code !== 200) {
                return layer.msg('上传失败');
            } else {
                //上传成功
                coverUrl3 = res.data;
            }
        }
        , error: function () {
            //演示失败状态，并实现重传
            const demoText = $('#demoText');
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
            demoText.find('.demo-reload').on('click', function () {
                uploadInst3.upload();
            });
        }
    });

    //封面图上传4
    let coverUrl4 = null;
    const uploadInst4 = upload.render({
        elem: '#test4'
        , url: host + '/a03/umsmember/uploadImage'
        , method: 'post'  //可选项。HTTP类型，默认post
        , field: 'multipartFile'
        , before: function (obj) {
            //预读本地文件示例，不支持ie8
            obj.preview(function (index, file, result) {
                $('#demo4').attr('src', result); //图片链接（base64）
            });
        }
        , done: function (res) {
            //如果上传失败
            if (res.code !== 200) {
                return layer.msg('上传失败');
            } else {
                //上传成功
                coverUrl4 = res.data;
            }
        }
        , error: function () {
            //演示失败状态，并实现重传
            const demoText = $('#demoText');
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
            demoText.find('.demo-reload').on('click', function () {
                uploadInst4.upload();
            });
        }
    });

    //封面图上传5
    let coverUrl5 = null;
    const uploadInst5 = upload.render({
        elem: '#test5'
        , url: host + '/a03/umsmember/uploadImage'
        , method: 'post'  //可选项。HTTP类型，默认post
        , field: 'multipartFile'
        , before: function (obj) {
            //预读本地文件示例，不支持ie8
            obj.preview(function (index, file, result) {
                $('#demo5').attr('src', result); //图片链接（base64）
            });
        }
        , done: function (res) {
            //如果上传失败
            if (res.code !== 200) {
                return layer.msg('上传失败');
            } else {
                //上传成功
                coverUrl5 = res.data;
            }
        }
        , error: function () {
            //演示失败状态，并实现重传
            const demoText = $('#demoText');
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
            demoText.find('.demo-reload').on('click', function () {
                uploadInst5.upload();
            });
        }
    });

    //封面图上传6
    let coverUrl6 = null;
    const uploadInst6 = upload.render({
        elem: '#test6'
        , url: host + '/a03/umsmember/uploadImage'
        , method: 'post'  //可选项。HTTP类型，默认post
        , field: 'multipartFile'
        , before: function (obj) {
            //预读本地文件示例，不支持ie8
            obj.preview(function (index, file, result) {
                $('#demo6').attr('src', result); //图片链接（base64）
            });
        }
        , done: function (res) {
            //如果上传失败
            if (res.code !== 200) {
                return layer.msg('上传失败');
            } else {
                //上传成功
                coverUrl6 = res.data;
            }
        }
        , error: function () {
            //演示失败状态，并实现重传
            const demoText = $('#demoText');
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
            demoText.find('.demo-reload').on('click', function () {
                uploadInst6.upload();
            });
        }
    });

    $.ajax({
        url: $.cookie("tempUrl") + "hSysDoctor/selectByPrimaryKey?id=" + $.cookie("doctorId"),
        type: "GET",
        success: function (result) {
            if (result.code === 200) {
                $(".doctorWork").attr("src", result.data.doctorWork);  //doctorWork
                $(".physicianQualification").attr("src", result.data.physicianQualification);  //资格证书
                $(".frontId").attr("src", result.data.frontId);  //frontId
                $(".reverseId").attr("src", result.data.reverseId);  //doctorWork
                $(".homePage").attr("src", result.data.homePage);  //homePage
                $(".video").attr("src", result.data.video);  //video

                form.render();
            } else {
                layer.msg(result.exception, {icon: 7, anim: 6});
            }
        }
    });

    //点击删除按钮事件
    $(".delBtn").click(function () {
        //获取要删除的dom
        let whatThis = $(this).attr("whatthis")
        console.log(this)
        //再次确认是否删除
        layer.confirm('确定删除?', {icon: 3, title: '提示'}, function (index) {
            //do something
            //执行删除
            $.ajax({
                url: $.cookie("tempUrl") + "hSysDoctor/updateByPrimaryKeySelective3",
                type: "PUT",
                datatype: "application/json",
                contentType: "application/json;charset=utf-8",
                data: JSON.stringify({
                    id: $.cookie("doctorId"),
                    doctorWork: whatThis === "doctorWork" ? "" : coverUrl,
                    physicianQualification: whatThis === "physicianQualification" ? "" : coverUrl2,
                    frontId: whatThis === "frontId" ? "" : coverUrl3,
                    reverseId: whatThis === "reverseId" ? "" : coverUrl4,
                    homePage: whatThis === "homePage" ? "" : coverUrl5,
                    video: whatThis === "video" ? "" : coverUrl6,
                }),
                success: function (result) {
                    if (result.code === 200) {
                        parent.layer.msg("删除成功");
                        window.location.reload()
                    } else {
                        layer.msg('删除异常', {icon: 7, anim: 6});
                    }
                }
            });

            layer.close(index);
        });
    });

    //点击保存按钮事件
    form.on("submit(addNews)", function (data) {
        $.ajax({
            url: $.cookie("tempUrl") + "hSysDoctor/updateByPrimaryKeySelective3",
            type: "PUT",
            datatype: "application/json",
            contentType: "application/json;charset=utf-8",
            data: JSON.stringify({
                id: $.cookie("doctorId"),
                doctorWork: coverUrl,
                physicianQualification: coverUrl2,
                frontId: coverUrl3,
                reverseId: coverUrl4,
                homePage: coverUrl5,
                video: coverUrl6,
            }),
            success: function (result) {
                if (result.code === 200) {
                    layer.msg("更新成功");
                } else {
                    layer.msg('更新异常', {icon: 7, anim: 6});
                }
            },
            error: function () {
                //防止点击过快，导致图片还未上传成功，再次延迟执行
                setTimeout(function () {
                    $.ajax({
                        url: $.cookie("tempUrl") + "hSysDoctor/updateByPrimaryKeySelective3",
                        type: "PUT",
                        datatype: "application/json",
                        contentType: "application/json;charset=utf-8",
                        data: JSON.stringify({
                            id: $.cookie("doctorId"),
                            doctorWork: coverUrl,
                            physicianQualification: coverUrl2,
                            frontId: coverUrl3,
                            reverseId: coverUrl4,
                            homePage: coverUrl5,
                            video: coverUrl6,
                        }),
                        success: function (result) {
                            if (result.code === 200) {
                                layer.msg("更新成功");
                            } else {
                                layer.msg('更新异常', {icon: 7, anim: 6});
                            }
                        },
                        error: function () {
                            layer.alert("您未做任何更改，或尝试再次点击", {icon: 7, anim: 6});
                        }
                    });
                }, 1500);
            }
        });
        return false;
    });
});